﻿using System;
using System.Data;
using Team4Mock.Entity;

namespace Team4Mock.UserControl.AddAccount
{
    public partial class Login : System.Web.UI.UserControl
    {
        UserEntity ue=new UserEntity();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Convert.ToString(Session["user"])))
                {
                    if (!string.IsNullOrEmpty(Convert.ToString(Session["role"])))
                    {
                        if (Session["role"].ToString().Equals("Admin"))
                        {
                            txtUser.Text = "";
                            txtPass.Text = "";
                            if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
                            {
                                gvAcc.DataSource = ue.GetUser();
                                gvAcc.DataBind();
                                pnAmend.Visible = true;
                                pnCreate.Visible = false;
                                int id = int.Parse(Request.QueryString["ID"]);
                                ue.UserID = id;
                                DataTable dt = new DataTable();
                                dt = ue.GetdataByID(ue);
                                if (dt.Rows.Count > 0)
                                {
                                    TextBox1.Text = dt.Rows[0][0].ToString();
                                    TextBox2.Text = dt.Rows[0][1].ToString();
                                    if (dt.Rows[0][2].ToString() == "Admin")
                                    {
                                        rAdmin.Checked = true;
                                        btnUpdate.Visible = false;
                                    }
                                    else
                                    {
                                        btnUpdate.Visible = true;
                                        rUser.Checked = true;
                                    }

                                }

                            }
                            else
                            {
                                pnAmend.Visible = false;
                                pnCreate.Visible = true;
                                btnUpdate.Visible = true;
                                gvAcc.DataSource = ue.GetUser();
                                gvAcc.DataBind();
                            }
                        }
                        else
                        {
                            Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "Rediect();", true);
                        }
                    }
                }
                else
                {
                    Response.Redirect("/FormView/Login.aspx");
                }
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
            {
                Response.Redirect("/FormView/Signup.aspx");
            }
            else
            {
                Response.Redirect("/FormView/Default.aspx");
            }
        }

        protected void btnCreate_Click(object sender, EventArgs e)
        {
            if (txtUser.Text==""|| txtPass.Text==""|| txtPass2.Text==""|| txtEmail.Text==""||txtCapcha.Text=="")
            {
                tb.Text = "Input madatory field";
            }
            else
            {
                if (txtPass.Text.Equals(txtPass2.Text))
                {
                    if (dlQuestion.SelectedIndex==0)
                    {
                        tb.Text = "Select Question";
                    }
                    else
                    {
                        if (txtCapcha.Text == Session["CaptchaImage"].ToString())
                        {
                            ue.ACCOUNT = txtUser.Text.Trim();
                            ue.PASSWORD = txtPass.Text.Trim();
                            ue.EMAIL = txtEmail.Text;
                            ue.Question = int.Parse(dlQuestion.SelectedValue);
                            ue.Answer = txtAnswer.Text;
                            ue.ROLE = rdAdmin.Checked ? "Admin" : "User";
                            ue.Insert(ue);
                            tb.Text = "Insert Success";
                            gvAcc.DataSource = ue.GetUser();
                            gvAcc.DataBind();
                        }
                        else
                        {
                            tb.Text = "Capcha not math";
                        }
                    }
                }
                else
                {
                    tb.Text = "Password not match";
                }
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
            {
                int id = int.Parse(Request.QueryString["ID"]);
                ue.UserID = id;
                ue.ROLE = rAdmin.Checked ? "Admin" : "User";
                ue.Update(ue);
                gvAcc.DataSource = ue.GetUser();
                gvAcc.DataBind();
                lb.Text = "Update success";
            }
        }
    }
}